package com.li.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.exception.BaseException;
import com.li.mapper.DeptMapper;
import com.li.pojo.dto.DeptDTO;
import com.li.pojo.po.Dept;
import com.li.pojo.vo.DeptVO;
import com.li.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024-04-11
 */
@Service
@Slf4j
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public int addDept(DeptDTO deptDTO) {
        Dept dept = new Dept();
        BeanUtils.copyProperties(deptDTO, dept);
        Dept info = deptMapper.selectById(dept.getParentId());
        if ("1".equals(info.getStatus())) {
            throw new BaseException("部门禁用，不允许新增");
        }
        dept.setUpdateBy("admin");
        dept.setCreateBy("admin");
        dept.setCreateTime(LocalDateTime.now().toString());
        dept.setUpdateTime(LocalDateTime.now().toString());

        dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
        return deptMapper.insert(dept);
    }


    @Override
    public List<Dept> getDeptList(String deptName, String status) {
        List<Dept> list = deptMapper.getDeptList(deptName,status);
        return list;
    }

    @Override
    public void updateDept(Dept dept) {
        if (dept.getParentId() == 0) {
            dept.setUpdateTime(LocalDateTime.now().toString());
            deptMapper.updateDept(dept);
            return;
        }
        Dept info = deptMapper.selectById(dept.getParentId());
        if ("1".equals(info.getStatus())) {
            throw new BaseException("该部门禁用，请换一个部门");
        }
        dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
        dept.setUpdateTime(LocalDateTime.now().toString());
        deptMapper.updateDept(dept);
    }

    @Override
    public void deleteById(Integer deptId) {
        Integer count = deptMapper.selectAncetors(deptId);
        if (count != 0) {
            throw new BaseException("部门内还有分部，不能删除");
        }
        removeById(deptId);
    }

    @Override
    public DeptVO getTreeSelect() {
        List<Dept> list = deptMapper.getAll();
        List<Integer> deptIdsList = new ArrayList<>();
        for (Dept dept1 : list) {
            deptIdsList.add(dept1.getDeptId());
        }
        DeptVO deptVO = null;
        for (Dept dept1 : list) {
            //说明是顶级部门
            if(!deptIdsList.contains(dept1.getParentId())){
                deptVO = new DeptVO();
                deptVO.setId(dept1.getDeptId());
                deptVO.setLabel(dept1.getDeptName());
                List<DeptVO> children= getChildren(list,dept1.getDeptId());
                deptVO.setChildren(children);
            }
        }
        return deptVO;
    }

    /**
     * 根据id查询对应部门树
     * @param roleId
     * @return
     */
    @Override
    public List<DeptVO> roleDeptTreeselect(Integer roleId) {
        List<Dept> list = deptMapper.getAll();
        Dept dept = getById(roleId);
        DeptVO deptVO = new DeptVO();
        deptVO.setId(deptVO.getId());
        deptVO.setLabel(dept.getDeptName());
        List<DeptVO> children = getChildren(list, roleId);
        deptVO.setChildren(children);
        List<DeptVO> deptVOS = new ArrayList<>();
        deptVOS.add(deptVO);
        return deptVOS;
    }

    /**
     * 递归查询
     * @param list
     * @param deptId
     * @return
     */
    private List<DeptVO> getChildren(List<Dept> list, Integer deptId) {
        Dept dept1 = getById(deptId);
        String ancestors = dept1.getAncestors();
        List<DeptVO> children = new ArrayList<>();
        for (Dept dept : list) {
            String ancestors1 = dept.getAncestors();
            //当本部门的总上级部门等与外层部门的总上级部门加上外层部门的部门id，说明是子节点（部门）
            if(ancestors1.equals(ancestors+","+dept1.getDeptId())){
                DeptVO deptVO = new DeptVO();
                deptVO.setId(dept.getDeptId());
                deptVO.setLabel(dept.getDeptName());
                List<DeptVO> children1 = getChildren(list, deptVO.getId());
                deptVO.setChildren(children1);
                children.add(deptVO);
            }
        }
       return children;
    }


}
